*************************************
*** Panel Analysis: GSS *************
*************************************

global OUT "SET_YOUR_OUTPATH"

// this is for data which is later read into R
global OUT2 "SET_YOUR_OUTPATH_FOR_DATAFRAMES"	


*create dummy datasets for regsave

*hybrid models
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_hhinc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_hhinc_dec_pp_sqrt.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_sat_fin.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_loginc.dta", replace


*bivariate
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_hhinc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_hhinc_dec_pp_sqrt_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_sat_fin_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_loginc_0.dta", replace


*equivalence testing
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\GSS\gss_coef_hhinc_dec_pp_sqrt_e.dta", replace


*load data
use "SET_YOUR_INPATH\GSS\gss_long.dta", clear


xtset id year, delta(2)

*use hhinc with standardized hhinc
egen zhhinc = std(hhinc)
replace hhinc = zhhinc


// use income deciles from 1-10
rename hhinc_dec10 hhinc_dec10_
clonevar hhinc_dec10 = hhinc_dec 
 
rename hhinc_dec_pp10 hhinc_dec_pp10_
clonevar hhinc_dec_pp10 = hhinc_dec_pp 
 
rename hhinc_dec_pp_sqrt10 hhinc_dec_pp_sqrt10_
clonevar hhinc_dec_pp_sqrt10 = hhinc_dec_pp_sqrt 


// create loginc
gen loginc = ln(hhinc_pp_sqrt)


*create interaction variables
gen hhinc0Xage = hhinc * age18
gen hhinc_pp0Xage = hhinc_pp * age18
gen hhinc_pp_sqrt0Xage = hhinc_pp_sqrt * age18

gen hhinc_decXage = hhinc_dec10 * age18
gen hhinc_dec_ppXage = hhinc_dec_pp10 * age18
gen hhinc_dec_pp_sqrtXage = hhinc_dec_pp_sqrt10 * age18

gen sat_finXage = sat_fin * age18

gen hhinc_decXage1835 = hhinc_dec10 * age1835
gen sat_finXage1835 = sat_fin * age1835
*

* generate income change/jobloss variables
gen incdiff = hhinc_dec-L.hhinc_dec 

recode incdiff (-9/-1=1 "shock")(0/9=0 "no shock")(else=.), gen(incdiff1)
recode incdiff (-9/-2=1 "shock")(-1 0/9=0 "no shock")(else=.), gen(incdiff2)


* labels

lab var int_intl "Int. international affairs"
lab var int_mil "Int. military affairs"


**************************************
***** Analysis ***********************
**************************************

estpost summarize conf_exec conf_ussc conf_congress int_intl int_mil ///
					hhinc hhinc_pp hhinc_pp_sqrt  hhinc_dec10 hhinc_dec_pp10 hhinc_dec_pp_sqrt10 sat_fin incdiff2 female age18 unemployed nlf college black race_other migrant ///
					hhinc_decXage sat_finXage hhinc_decXage1835 hhinc_dec_ppXage hhinc_dec_pp_sqrtXage sat_finXage1835
esttab using "$OUT\Tables\GSS\summary_stats_gss.rtf", b(2) cells("count mean(fmt(3)) sd(fmt(3))  min max") replace


* regressions with age (continuous)

// for coef export
foreach var of varlist int_intl int_mil {
	xthybrid `var' hhinc, clusterid(id) full
	eststo m001_`var'
	regsave W__hhinc B__hhinc using "$OUT2\GSS\gss_coef_hhinc_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc female age18 unemployed nlf college black race_other migrant , clusterid(id) full
	eststo m01_`var'
	regsave W__hhinc B__hhinc using "$OUT2\GSS\gss_coef_hhinc.dta", ci level(95) autoid append
	
	xthybrid `var' loginc, clusterid(id) full
	eststo m002_`var'
	regsave W__loginc B__loginc using "$OUT2\GSS\gss_coef_loginc_0.dta", ci level(95) autoid append
	xthybrid `var' loginc female age18 unemployed nlf college black race_other migrant , clusterid(id) full
	eststo m02_`var'
	regsave W__loginc B__loginc using "$OUT2\GSS\gss_coef_loginc.dta", ci level(95) autoid append
	
	
	xthybrid `var' hhinc_dec_pp_sqrt10, clusterid(id) full
	eststo m03_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\GSS\gss_coef_hhinc_dec_pp_sqrt_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 unemployed nlf college black race_other migrant , clusterid(id) full
	eststo m3_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\GSS\gss_coef_hhinc_dec_pp_sqrt.dta", ci level(95) autoid append
	
	xthybrid `var' sat_fin, clusterid(id) full
	eststo m04_`var'
	regsave W__sat_fin B__sat_fin using "$OUT2\GSS\gss_coef_sat_fin_0.dta", ci level(95) autoid append
	xthybrid `var' sat_fin female age18 unemployed nlf college black race_other  migrant , clusterid(id) full
	eststo m4_`var'
	regsave W__sat_fin B__sat_fin using "$OUT2\GSS\gss_coef_sat_fin.dta", ci level(95) autoid append
}

// for equivalence testing
foreach var of varlist int_intl int_mil  {
	xtreg `var' hhinc_dec_pp_sqrt10 age18 unemployed nlf, fe
	regsave hhinc_dec_pp_sqrt10 using "$OUT2\GSS\gss_coef_hhinc_dec_pp_sqrt_e.dta", ci level(90) autoid append
}	



// interactions
foreach var of varlist int_intl int_mil {
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 unemployed nlf college black race_other  migrant hhinc_dec_pp_sqrtXage, clusterid(id) full
	eststo m7_`var'
}

*tables
esttab m3_int_intl m7_int_intl m3_int_mil m7_int_mil ///
	using "$OUT\Tables\GSS\hybrid_all_incdec_pp_sqrt.rtf", b(3) se replace	
esttab m3_int_intl m7_int_intl m3_int_mil m7_int_mil ///
	using "$OUT\Tables\GSS\hybrid_all_incdec_pp_sqrt.tex", ///
	b(3) se replace label nonumbers scalars("N N") booktabs alignment(D{.}{.}{3}) ///
	varlabels ///
				(R__sex "Sex" ///
				R__migrant "Migrant" ///
				R__college "Edu: college" ///
				R__black "Race: black" ///
				R__race_other "Race: other" ///
				W__age18 "W: Age" ///
				W__unemployed "W: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__age18 "B: Age" ///
				B__unemployed "B: Unemployed" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				W__sat_finXage "W: Sat. Inc.*Age" ///
				B__sat_finXage "B: Sat. Inc.*Age" ///
				_cons "Constant")
				


// test: objective income on subjective income
foreach var of varlist hhinc hhinc_dec_pp_sqrt10 incdiff2 {
	xthybrid sat_fin `var' female age18 unemployed nlf college black race_other migrant, clusterid(id) full
	eststo so_`var'
}
esttab so_hhinc so_hhinc_dec_pp_sqrt10 so_incdiff2 ///
	using "$OUT\Tables\GSS\hybrid_all_inc_subj_obj.rtf", b(2) se replace
esttab so_hhinc so_hhinc_dec_pp_sqrt10 so_incdiff2 ///
	using "$OUT\Tables\GSS\hybrid_all_inc_subj_obj.tex", b(2) se replace booktabs alignment(D{.}{.}{3})	


	
* vote as logistic
foreach var of varlist int_intl int_mil {
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 unemployed nlf college black race_other migrant , clusterid(id) family(binomial) link(logit) full
	eststo m5_`var'_logit
}
*

esttab m5_int_intl_logit m5_int_mil_logit using "$OUT\Tables\GSS\hybrid_all_logit.rtf", b(3) se replace
esttab m5_int_intl_logit m5_int_mil_logit using "$OUT\Tables\GSS\hybrid_all_logit.tex", ///
	b(3) se replace label nonumbers scalars("N N") booktabs alignment(D{.}{.}{3}) ///
	varlabels ///
				(R__sex "Sex" ///
				R__migrant "Migrant" ///
				R__college "Edu: college" ///
				R__black "Race: black" ///
				R__race_other "Race: other" ///
				W__age18 "W: Age" ///
				W__unemployed "W: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__age18 "B: Age" ///
				B__unemployed "B: Unemployed" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				W__sat_fin "W: Sat. Income" ///
				B__sat_fin "B: Sat. Income" ///
				_cons "Constant")











